package com.donlim.dev.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.donlim.dev.dto.LoginParams;
import com.donlim.dev.dto.PageInfoDto;
import com.donlim.dev.dto.sql.QueryWrapperAndPage;
import com.donlim.dev.dto.sql.SQLParams;
import com.donlim.dev.entity.SysUser;
import com.donlim.dev.enums.ExceptionCode;
import com.donlim.dev.exception.SysUserException;
import com.donlim.dev.mapper.SysUserMapper;
import com.donlim.dev.service.SysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.donlim.dev.utils.JwtTokenUtil;
import com.donlim.dev.utils.SQLUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 星
 * @since 2020-11-03
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Value("${jwt.tokenHeader}")
    private String tokenHeader;

    @Override
    public String login(String userCode,String password) throws SysUserException {
        SysUser sysUser = loadUserByUserCode(userCode);
        if (sysUser != null){
            if (!sysUser.getIsEnable()) throw new SysUserException(ExceptionCode.USER_DISABLED.getCode(),ExceptionCode.USER_DISABLED.getMessage());
            if (!password.equals(sysUser.getPassword()) || StringUtils.isEmpty(password)){
                throw new SysUserException(ExceptionCode.WRONG_USER.getCode(),ExceptionCode.WRONG_USER.getMessage());
            }
        }
        String token = jwtTokenUtil.generateToken(sysUser);
        return token;
    }


    @Override
    public SysUser loadUserByUserCode(String userCode) throws SysUserException {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<SysUser>()
                .eq("UserCode",userCode);
        SysUser sysUser = getOne(queryWrapper);
        if (sysUser == null) throw new SysUserException(ExceptionCode.WRONG_USER.getCode(),ExceptionCode.WRONG_USER.getMessage());
        return sysUser;
    }

    @Override
    public SysUser getUserInfo(HttpServletRequest request) throws SysUserException {
        String token = request.getHeader(tokenHeader);
        String userCodeFromToken = jwtTokenUtil.getUserCodeFromToken(token);
        SysUser sysUser = loadUserByUserCode(userCodeFromToken);
        return sysUser;
    }

    @Override
    public PageInfoDto<SysUser> list(SQLParams sqlParams) {
        QueryWrapperAndPage queryWrapperAndPage = SQLUtils.getQueryWrapperAndPage(sqlParams);
        QueryWrapper queryWrapper = queryWrapperAndPage.getQueryWrapper();
        PageInfoDto pageInfoDto = new PageInfoDto();
        if (!sqlParams.isPage()) {
            List<SysUser> list = list(queryWrapper);
            pageInfoDto.setRecords(list);
            pageInfoDto.setTotal(list.size());
        }else {
            Page page = queryWrapperAndPage.getPage();
            Page iPage = page(page);
            BeanUtils.copyProperties(iPage,pageInfoDto);
        }
        return pageInfoDto;
    }


}
